package com.qf.dbUtil.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dbUtil.common.exception.AppException;
import com.qf.dbUtil.common.result.ResponseEnum;
import com.qf.dbUtil.entity.User;
import com.qf.dbUtil.entity.UserExample;
import com.qf.dbUtil.mapper.UserMapper;
import com.qf.dbUtil.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 功能说明
 *
 * @author yihuan
 * @data 2020-10-12 17:04:42
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public User findUserById(Integer id) {
        log.info("service info");
        log.debug("service debug");
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<User> findAllPaging(int pageno, int pagesize) {
        PageHelper.startPage(pageno,pagesize);
        List<User> users = userMapper.selectByExample(null);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public User login(String userName, String passWord) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(userName);
        List<User> users = userMapper.selectByExample(userExample);
        if(users == null || users.size() == 0){
            throw new AppException(ResponseEnum.LOGIN_USERNAME_NO_EXISTS);
        }
        User user = users.get(0);
        if(user.getPassword().equals(passWord) == false){
            throw new AppException(ResponseEnum.LOGIN_PASSWORD_WRONG);
        }
        return user;
    }

    /**
     * @Transactional 事务管理
     * 产生运行时异常跟错误时进行回滚
     * rollbackFor：触发回滚的异常，默认是RuntimeException和Error
     * isolation: 事务的隔离级别，默认是Isolation.DEFAULT也就是数据库自身的默认隔离级别，
     * 比如MySQL是ISOLATION_REPEATABLE_READ可重复读
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class,Error.class})
    public Integer getMoney(User user, BigDecimal money) {
        //查询用户名是否存在
        User user1 = userMapper.selectByPrimaryKey(user.getId());
        if(user1 == null){
            throw new AppException(ResponseEnum.LOGIN_USERNAME_NO_EXISTS);
        }
        //查询余额是否充足
        if(user.getCount().compareTo(money) == -1){
            throw new AppException(ResponseEnum.MONEY_NOT_ENOUTH);
        }
        //余额充足，更新余额，取钱
        //updateByExampleSelective()更新想更新的字段，updateByExample()更新所有的字段
        // bignum1.subtract(bignum2);  bigdecimal减法
        user.setCount(user.getCount().subtract(money));
        int i = userMapper.updateByPrimaryKeySelective(user);
        return i;
    }

    @Override
    public User findByPhone(String phone) {
        UserExample userExample = new UserExample();
        userExample.createCriteria()
                .andPhoneEqualTo(phone);
        List<User> users = userMapper.selectByExample(userExample);
        System.out.println("service"+users.get(0));
        if(users.get(0)==null){
            throw new AppException(ResponseEnum.USER_NO_FIND);
        }
        return  users.get(0);
    }

}
